require("apm_core")
require("ApmXml")

local G_DEBUG = false

if G_DEBUG == false then
	function logInfo(fmt, ...) end
end

local s_type_code
local s_type_name
local s_seqno
local s_ret_code
local s_err_msg

local function message_parser(s_msg, ApmData, is_req)
	local t = {}
	if s_msg == nil then return t end
	if string.len(s_msg) == 0 then return t end

	local s_key = is_req and "request.trans." or "response.trans."
	for k, v in string.gmatch(s_msg, "([_a-zA-z]+)@%d+@%d+@@<!%-%-(.-)%-%->") do
--		logInfo("[" .. k .. "=" .. v .. "]")
		t[k] = v
		ApmData.replaceField(s_key .. k, v)
	end
	return t
end

local function parse_request(s_msg, ApmData)
	local t_kv = message_parser(s_msg, ApmData, true)
	if t_kv == nil then return end
	if s_type_code == nil then s_type_code = t_kv["_jym"] end
	if s_type_name == nil then s_type_name = t_kv["JYM"] end
	if s_type_name == nil then s_type_name = s_type_code end

	if s_seqno == nil then s_seqno = t_kv["_hostseqno"] end
	if s_seqno == nil or s_seqno == " " or string.len(string.trim(s_seqno)) == 0 then
		s_seqno = t_kv["_gyh"]
	end
end

local function parse_response(s_msg, ApmData)
	local t_kv = message_parser(s_msg, ApmData, false)
	if t_kv == nil then return end
	if s_ret_code == nil then s_ret_code = t_kv["_hostcode"] end
	if s_err_msg == nil then s_err_msg = t_kv["_errmsg"] end
end

function post_analysis()
	local ApmData = require("ApmData")
	
	if ApmData.getMsgType() ~= 1 then return end

	local req_stub = ApmData.getField("request.rpc.arg.1", 1)
	local resp_stub = ApmData.getField("response.rpc.return", 1)
	parse_request(req_stub, ApmData)
	parse_response(resp_stub, ApmData)
	--[[
	if req_stub ~= nil and resp_stub ~= nil then
		logInfo("(%s:%d->%s:%d) req_stub:[%s] resp_stub:[%s] [%d:%d]",
			ApmData.getSrcIp(),
			ApmData.getSrcPort(),
			ApmData.getDstIp(),
			ApmData.getDstPort(),
			req_stub,
			resp_stub,
			string.len(req_stub),
			string.len(resp_stub))
	end
	if req_stub ~= nil and resp_stub ~= nil and string.len(req_stub) ~= 0 and string.len(resp_stub) ~= 0 then
		logInfo("(%s:%d->%s:%d) req_stub:[%s] resp_stub:[%s]",
			ApmData.getSrcIp(),
			ApmData.getSrcPort(),
			ApmData.getDstIp(),
			ApmData.getDstPort(),
			req_stub,
			resp_stub)
	end
	]]
	if s_type_code ~= nil and s_type_name ~= nil then
		ApmData.replaceField("transaction.type_code", s_type_code)
		ApmData.replaceField("transaction.type_name", s_type_name)
		if s_ret_code ~= nil then
			ApmData.replaceField("response.code_string", s_ret_code)
			ApmData.delFields("response.code")
		end
		if s_err_msg ~= nil then
			ApmData.replaceField("response.error_message", s_err_msg)
		end
		logInfo("(%s:%d->%s:%d) JYM:%s|_jym:%s|_hostseqno:%s|_hostcode:%s|_errmsg:%s",
			ApmData.getSrcIp(),
			ApmData.getSrcPort(),
			ApmData.getDstIp(),
			ApmData.getDstPort(),
			s_type_name,
			s_type_code,
			s_seqno,
			s_ret_code,
			s_err_msg)
	else
		logInfo("(%s:%d->%s:%d) req_stub:[%s] resp_stub:[%s] [%d:%d]",
			ApmData.getSrcIp(),
			ApmData.getSrcPort(),
			ApmData.getDstIp(),
			ApmData.getDstPort(),
			req_stub,
			resp_stub,
			req_stub ~= nil and string.len(req_stub) or 0,
			resp_stub ~= nil and string.len(resp_stub) or 0)
	end
end
-- v1.3
